package com.itsu.mongodb.service.impl;

import com.itsu.interfaces.entity.RecommendUser;
import com.itsu.interfaces.service.RecommendUserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        return mongoTemplate.findOne(Query.query(Criteria.where("toUserId").is(toUserId))
                        .with(Sort.by(Sort.Order.desc("score")))
                        .limit(1)
                , RecommendUser.class);
    }

    @Override
    public List<RecommendUser> getRecommendsByUid(Long uid) {
        return mongoTemplate.find(Query.query(Criteria.where("toUserId").is(uid)), RecommendUser.class);
    }

    @Override
    public RecommendUser queryScore(Long userId, Long toUserId) {
        return mongoTemplate.findOne(Query.query(Criteria.where("toUserId").is(toUserId)
                        .and("userId").is(userId))
                , RecommendUser.class);
    }
}
